// twitter bootstrap

var gameData = {};
var playerData = {};

gameData.colors = ["#cc1100", "#22aa00", "#2211cc"];
playerData.flag_color = "#cc1100";
gameData.crosses = ["None", "Normal", "Corner"];
playerData.flag_cross = "None";
gameData.currentPlayer = false;

$(function() {
    $( "#select_color" ).selectable({
        stop: function() {
	        //var result = $( "#flag" );
	        $( ".ui-selected", this ).each(function() {
		        var index = $( "#select_color li" ).index( this );
		        playerData.flag_color = gameData.colors[index];
		        drawFlag("flag");
	        });
        }
    });
});

$(function() {
    $( "#select_cross" ).selectable({
        stop: function() {
	        //var result = $( "#flag" );
	        $( ".ui-selected", this ).each(function() {
		        var index = $( "#select_cross li" ).index( this );
		        playerData.flag_cross = gameData.crosses[index];
		        drawFlag("flag");
	        });
        }
    });
});

var map = new Image();   // Create new img element

$(document).ready(function (){
	showMap();
	var newAdmin = false;
	$.getJSON("isnewadmin", function(data) {
		if (data) {
			$.post("createadmin");
			$("#newAdminPopup").css("display", "inline").click( function () {
				$(this).fadeOut(200);
			});
		}
	});
    $("ul.topnav li").click(function() { //When trigger is clicked...  
        
	    //Following events are applied to the subnav itself (moving subnav up and down)  
	    $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
	  
        $(this).parent().hover(function() {  
        }, function(){    
            $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
        });  
  
        //Following events are applied to the trigger (Hover events for the trigger)  
    }).hover(function() {   
        $(this).addClass("subhover"); //On hover over, add class "subhover"  
    }, function(){  //On Hover Out  
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
    });
    
    $("#createplayer").click(createPlayer);
	var playerdataform = $("#playerdataform");
	
	//bind the `submit` event
	playerdataform.on("submit", function(event) {
			event.preventDefault();
			// serialize the form
		if ($("#name").val() === "" || $("#name").val() === null) {
			alert("Hey, at least one letter, otherwise its obviously not a name")
		} else {
			var data = playerdataform.serialize();
			data = data + "&flagcolor=" + playerData.flag_color + "&flagcross=" + playerData.flag_cross
			 // POST the data to the server using the form's action URL
			$.post(playerdataform.attr("action"), data, function(data) {
			     // Data sent, let's clear the form elements
				 playerdataform.find("#name").val("");
				 $("#player_selector").css("display", "none");
				 setCurrentPlayer(data);
			});
		}
	});
});

function setCurrentPlayer(data) {
	console.log(data);
	$("#player_name").html(gameData.playerInfo.data);
	gameData.playerInfo.setPlayer(data);
	gameData.playerInfo.trigger("player_info:show_player_data");
}

function createPlayer() {
	console.log("I am the cloud!");
	$("#player_selector").css("display", "inline");
	drawFlag("flag");
	drawCrosses();
}

function showMap() {
    
	var R = Raphael("inTheCloud", 700, 700);
	var attr = {
	stroke: "#000",
	fill: "#333",
	"stroke-width": 1,
	"stroke-opacity": 0,
	"fill-opacity": 0,
	"stroke-linejoin": "round"
	};

	var svgmap = {};
	R.setViewBox(55,38,700,700);
	svgmap.dalarne = {path: R.path("m 401.16119,44.084595 0.42997,18.488672 4.72966,12.899074 3.43975,8.599382 -3.86972,3.439753 0.85994,4.299691 8.16941,3.439753 0.85994,8.16941 -4.72966,12.03914 -6.87951,7.30947 -8.16941,6.87951 -9.02935,3.43975 -11.1792,3.43976 -11.60916,-1.28991 -7.30948,-4.72966 -6.87951,-5.5896 -9.02935,-1.28991 -9.02935,-1.71987 -10.31926,-1.28991 -6.44953,-0.42997 -5.5896,2.57981 -6.44954,1.71988 -10.31926,0.42997 -3.86972,0 -3.86972,0 -8.16942,-8.59938 -2.14984,-3.43976 -3.86972,-12.03913 -9.02936,-9.459322 -12.4691,-6.019567 -7.30948,-9.88929 -4.72966,-7.309475 -6.01956,-3.009784 -6.01957,-6.879506 4.72966,-3.439753 -3.43975,-12.899074 z").attr(attr),
	    	name: "Dalarna"};
	svgmap.roslagen = {path: R.path("m 415.78014,96.110859 4.29969,7.739441 12.03914,-3.43975 7.73944,9.88929 7.73944,4.29969 14.61895,24.93821 0,16.7688 9.02936,0 2.57981,5.58959 -7.73944,5.5896 -3.86973,4.29969 -3.00978,3.86973 -2.14984,3.00978 -12.89908,3.86972 3.00979,10.74923 -14.61895,2.14985 -3.43976,-9.88929 0,-4.72966 -7.73944,-3.00979 -6.01957,0 -6.01957,3.00979 -4.72966,-3.86973 -0.85994,-19.34861 -21.49845,-1.2899 -1.71988,-20.63852 13.75901,-3.86972 14.61895,-13.32905 6.44954,-14.18898 z").attr(attr),
	    	name: "Roslagen"};
	svgmap.wland = {path: R.path("m 284.63956,124.05885 17.19876,0.42997 12.89908,-3.86972 14.61895,0.85994 20.63851,3.43975 6.87951,5.15963 5.15963,5.15963 13.75901,1.71988 4.29969,-0.85994 2.14985,19.77858 19.77858,2.14984 3.00978,19.34861 -25.36818,-1.71987 -6.44953,3.86972 -14.18898,0.85994 -0.85994,6.8795 -3.00979,0.85994 -3.43975,-4.29969 -17.62873,-8.59938 -12.46911,-1.71988 -3.43975,-9.45932 0,-10.74923 -7.73944,-8.59938 -9.02936,-3.43975 -8.16941,-1.28991 -6.01957,-5.15963 z").attr(attr),
	    	name: "Vastmanland"};
	svgmap.wermland = {path: R.path("m 224.87385,60.853391 -9.02935,-1.719877 -7.73945,4.299691 3.43976,8.599383 2.14984,8.599382 2.14985,10.749228 5.15963,14.188982 0,12.4691 1.71987,13.32905 1.28991,12.03913 -6.01957,2.14985 -5.15963,9.45932 -5.15962,7.30947 -8.59939,0 -2.57981,8.59939 4.29969,24.07827 -1.71988,3.86972 -9.45932,1.71987 0,5.15963 -3.43975,4.72966 1.28991,12.89908 3.43975,6.44954 0.42997,13.45932 2.57981,7.17919 -0.42997,14.18898 10.74923,6.01957 10.31926,-8.59938 -0.85994,-4.29969 4.72966,-1.28991 3.00979,-7.30947 3.86972,-3.43976 -0.42997,-6.44953 -7.73944,-0.85994 0.42996,-13.75901 6.44954,6.01956 1.28991,-7.30947 3.00978,-7.73945 5.5896,-1.71987 8.59938,2.57981 0,6.87951 2.57982,4.72966 4.72966,0 1.71988,-3.43975 -0.42997,-5.15963 -2.57982,-5.15963 -3.86972,-8.59939 0,-4.29969 2.14985,-6.44953 7.30947,-2.57982 6.44954,0.42997 12.4691,-1.71988 3.43975,6.01957 0,7.30948 0.85994,9.88929 1.71988,6.01956 7.30947,0 7.30948,-0.85993 0.42997,-3.86973 0,-7.73944 -2.14985,-6.87951 -1.71987,-8.16941 0.42997,-6.01957 0,-3.86972 -3.43976,-3.86972 9.02935,-6.87951 4.72966,-6.8795 7.73945,-7.30948 8.16941,-3.43975 -5.5896,-10.31926 -7.30947,-3.00978 -10.31926,-1.28991 -9.02935,-6.87951 -0.42997,-7.73944 -7.73945,-8.16941 -4.72966,-16.33883 -9.88929,-9.459322 -13.75901,-5.589598 -10.31926,-17.628734 -7.30947,-3.439753 z").attr(attr),
	    	name: "Warmland"};
	svgmap.nerike = {path: R.path("m 317.74718,153.72672 -8.59938,3.43975 -8.16942,7.73945 -4.29969,8.16941 -9.45932,5.5896 3.86972,4.29969 0,10.31926 3.00979,13.75901 -0.42997,11.60917 3.43975,13.32904 13.32905,3.86972 11.17919,-3.43975 10.74923,-3.00978 7.73944,-1.71988 6.87951,1.28991 5.5896,-0.42997 0.42997,-5.5896 0.42997,-9.88929 4.72966,-0.85994 -0.85994,-8.16941 -6.01957,-5.15963 4.29969,-5.15963 -0.42997,-6.44954 -4.29969,-5.15963 -16.33882,-6.8795 -12.89908,-2.14985 -4.72966,-9.02935 z").attr(attr),
	    	name: "Nerike"};
	svgmap.sland = {path: R.path("m 446.73792,196.72363 3.43975,4.72966 4.72966,1.28991 -3.43975,6.01957 -4.29969,4.72966 -2.57982,3.86972 -0.42997,6.44954 -7.73944,3.86972 -3.43976,-1.28991 -3.86972,-8.16941 -6.01957,0 -3.43975,9.45932 -4.72966,2.14985 -4.72966,8.16941 -13.75901,-4.29969 -9.02935,-4.29969 -12.46911,-2.57982 -12.03913,-0.85994 -11.1792,-0.85994 -0.42997,-11.60916 7.73945,-1.28991 -0.42997,-8.16941 -6.87951,-4.29969 4.29969,-6.01957 0,-4.72966 2.57982,-2.57982 10.31926,0 6.01956,0 2.14985,3.43976 12.03913,-2.57982 5.15963,2.57982 5.15963,0 9.88929,4.72966 6.44954,1.71987 2.57981,2.14985 4.72966,-2.57982 8.59939,2.14985 z").attr(attr),
	    	name: "Sormland"};
	svgmap.ogota = {path: R.path("m 315.16737,234.56092 2.14984,13.32904 3.43975,13.32904 -9.88929,1.71988 c 0,0 -6.01956,12.4691 -5.58959,10.74923 0.42996,-1.71988 -2.14985,11.60916 -2.14985,11.60916 l -2.57981,6.44954 8.16941,5.15963 0,6.44953 8.16941,1.28991 0,6.01957 9.02935,2.14985 5.15963,2.14984 3.43976,8.59938 8.59938,0.85994 2.57981,2.57982 7.73945,0.42997 9.02935,7.73944 c 0,0 5.5896,-2.14985 7.30947,-2.14985 1.71988,0 10.74923,1.71988 10.74923,1.71988 0,0 2.14985,0.85994 3.43975,-2.57981 1.28991,-3.43976 1.28991,-13.75902 1.28991,-13.75902 0,0 -0.42997,-2.14984 0.42997,-4.29969 0.85994,-2.14984 0,-5.15963 0,-5.15963 l 5.15963,-27.51802 -2.57981,-7.73944 -3.43976,-4.2997 6.01957,-4.29969 6.87951,-3.86972 -1.71988,-9.88929 -9.45932,-3.43975 10.31926,-1.71988 c 0,0 2.14984,-0.42997 4.29969,-0.85994 2.14985,-0.42997 7.30947,-6.01956 7.30947,-6.01956 l -11.17919,-4.2997 -11.1792,-4.72966 -11.60917,-3.00978 -19.77858,-1.71988 -2.57981,1.71988 -12.4691,0.85994 -13.75902,3.86972 z").attr(attr),
	    	name: "Ostergotland"};
	svgmap.vgota = {path: R.path("m 203.80536,272.3982 -3.00978,11.60916 -2.14985,0 0,8.16942 -3.43975,1.71987 -8.59938,0 0.42997,7.30948 -3.86972,2.14984 2.14984,3.86973 -0.42997,4.72966 c 0,0 8.59938,-1.28991 5.5896,5.15963 -3.00978,6.44953 -3.00978,6.44953 -3.00978,6.44953 l 8.16941,1.71988 7.73944,3.00978 8.59939,3.43976 3.00978,10.74922 2.14985,10.31926 1.2899,4.72966 16.33883,2.57982 6.01957,0.85994 5.5896,-12.03914 8.16941,-3.43975 0,-3.43975 9.45932,1.2899 7.30948,-25.79814 3.43975,-6.44954 6.44953,0.42997 3.43976,-21.06849 0,-13.75901 11.60916,-11.60917 2.57982,-18.0587 9.45932,-10.74923 -10.74923,-3.86972 -4.29969,-11.1792 -13.32904,-1.2899 -0.85994,16.76879 -6.01957,6.87951 -21.06849,15.47889 -7.73944,0.42996 -0.85994,-6.44953 -4.72966,0 -3.43975,8.16941 -8.59938,8.16941 -4.72966,2.57982 -11.1792,-4.72966 z").attr(attr),
	    	name: "Vastergotland"};
	svgmap.smaland = {path: R.path("m 300.54842,292.17678 0,13.32904 -4.72967,0 -3.43975,14.18898 -5.5896,2.57982 -5.5896,0.42997 c 0,0 0.42997,-6.87951 0,-9.02936 -0.42996,-2.14984 -8.59938,-1.2899 -8.59938,-1.2899 l -8.16941,30.09783 -8.59938,0 -0.42997,6.01957 -8.16942,0 -3.86972,10.74923 -5.15963,14.61895 8.16942,6.44954 4.72966,9.88929 3.43975,13.32904 -2.57982,4.72966 10.31926,12.4691 11.1792,7.30948 15.90886,-3.00978 c 0,0 6.01957,0.42996 9.02935,-1.71988 3.00978,-2.14985 9.02935,-5.15963 9.02935,-5.15963 l 17.19877,-1.71988 6.8795,2.14985 17.19877,1.71988 7.73944,-3.00979 10.74923,-0.42997 4.29969,-1.2899 1.28991,-17.19877 6.44953,-1.28991 -2.14984,-10.31926 0,-7.73944 4.29969,-8.59938 -0.42997,-9.45932 -1.28991,-6.44954 3.43976,-7.30948 7.73944,-6.44953 -4.29969,-7.30948 -15.47889,0 -8.16941,1.71988 -8.16942,-7.30948 -7.73944,0 -4.29969,-3.00978 -8.59938,0 -4.72966,-10.31926 -10.31926,-3.00978 -3.86972,-7.30948 -6.01957,0 -1.28991,-6.44953 z").attr(attr),
	    	name: "Smaland"};
	svgmap.skane = {path: R.path("m 188.32647,325.2844 2.14985,10.31926 -1.28991,6.44954 9.02935,3.00978 2.14985,20.63852 8.16941,9.02935 0,6.44954 18.91864,19.34861 1.71988,15.90885 -7.30947,-0.85993 0.42997,6.8795 3.43975,8.16941 0,5.5896 -5.15963,0 -5.15963,-3.86972 -1.28991,4.72966 5.15963,8.16941 2.57982,6.87951 2.14984,4.29969 -0.42997,5.5896 5.15963,6.44954 3.00979,7.30947 -9.02936,4.72966 3.86973,8.59938 8.59938,11.1792 2.14984,4.29969 15.04892,0 6.44954,-3.00978 24.07827,-1.28991 4.72966,-5.15963 5.15963,-5.5896 -6.01957,-3.43975 0,-7.73944 0.42997,-6.44954 -5.5896,-1.71988 8.16942,-6.44953 3.00978,-7.73945 2.57982,-9.88929 10.74922,-4.29969 18.05871,-3.43975 17.19876,0.42997 9.45932,0 4.29969,8.16941 5.15963,-0.42997 2.14985,-12.89907 1.28991,-3.86972 4.72966,-12.89908 -13.32905,3.43976 -9.45932,0.85993 -14.18898,-0.42997 -5.5896,-4.72966 -18.48867,3.43976 -10.31926,6.01956 -9.88929,0.85994 -12.4691,2.14985 -10.31926,-6.87951 -13.75901,-12.89907 4.72966,-4.72966 -3.86972,-12.89907 -4.2997,-9.45933 -9.88929,-8.59938 5.5896,-13.75901 -25.36818,-3.00978 -6.8795,-23.64831 z").attr(attr),
	    	name: "Skane"};
	svgmap.sjelland = {path: R.path("m 180.58703,516.62066 12.03914,-0.42997 0.42997,-5.5896 3.43975,-4.29969 9.02935,0.42997 3.43975,0.42997 0.42997,-21.06849 3.86972,-11.60917 2.57982,-6.44953 -3.00979,-7.73945 5.15963,-9.02935 -4.29969,-3.00978 -5.15963,0.42997 -3.43975,-4.72966 -6.87951,0.85994 -6.44953,8.59938 4.72966,3.43975 -3.86972,4.29969 -3.86973,-3.00978 -5.58959,-3.00979 -0.42997,8.59939 1.2899,9.45932 -3.00978,2.14984 -2.57981,-6.8795 -6.01957,0.42997 2.14984,-7.73945 0,-6.8795 -7.73944,0 0,4.29969 0.42997,4.72966 -4.72966,0 -2.57982,5.5896 -10.31926,0 -6.8795,5.15963 8.16941,8.16941 0.85994,23.21833 13.32904,6.01957 z").attr(attr),
	    	name: "Sjalland"};
	svgmap.jylland = {path: R.path("m 76.534503,516.19069 2.579815,-10.74923 8.599383,-0.85994 -1.289908,-10.31926 -1.289907,-6.01956 8.599382,0.42997 1.289908,9.88929 8.169414,8.16941 2.14984,10.74923 28.80794,-0.42997 0,-9.02935 0,-8.59939 -2.14985,-8.59938 1.28991,-8.16941 -4.29969,-0.85994 -2.14985,5.5896 -4.29969,2.14984 -1.71988,-5.58959 -3.43975,-0.85994 -1.28991,-3.86973 -13.32904,-0.85993 -2.57981,4.29969 -7.739449,0.42997 0,-6.01957 -5.15963,-0.85994 0,-8.16941 7.739445,1.28991 11.179194,-2.14985 -3.43975,-10.31926 7.30947,-0.42997 6.44954,-6.01957 0.85994,-7.30947 1.71988,-6.44954 0,-6.44953 6.44953,1.71987 14.61895,1.71988 0.85994,-12.46911 5.5896,-5.58959 0,-2.57982 -5.15963,-6.44954 -16.7688,0 -6.8795,-10.31926 0.85994,-7.30947 0,-15.90886 12.03913,-19.77858 2.14985,-16.33882 -2.57982,-8.16942 -3.00978,-8.16941 -7.73944,0.42997 -7.73945,0.42997 -6.44954,4.72966 -0.85993,10.74923 -13.759016,8.59938 -4.299691,7.30947 -4.299691,3.43976 -12.469104,-3.00979 -6.449537,6.44954 -8.169414,-0.42997 0.42997,157.3687 z").attr(attr),
	    	name: "Jylland"};
	svgmap.gotland = {path: R.path("m 457.05718,381.61035 6.44953,-6.01956 -1.71987,-5.15963 -1.28991,-6.87951 8.59938,-4.29969 4.29969,-12.03914 0.42997,-24.9382 11.1792,-6.87951 -0.42997,-8.16941 3.86972,-4.29969 5.5896,-3.43976 6.87951,2.57982 -2.57982,-5.5896 c 0,0 -2.57981,-0.85994 -6.87951,-1.71988 -4.29969,-0.85994 -9.88929,8.16942 -9.88929,8.16942 l -10.74922,-3.00979 -4.72966,8.59938 -12.89908,0.85994 8.59939,9.45932 -17.62874,8.16942 2.14985,18.91864 0.85994,6.44953 9.02935,7.73945 -2.14985,17.62873 z").attr(attr),
	    	name: "Gotland"};
	svgmap.livland = {path: R.path("m 593.35739,514.90078 -0.42997,-8.16941 2.14984,-9.88929 -1.71987,-14.18898 0,-9.02935 -8.16942,-13.75902 -0.85994,-15.90885 -8.16941,-8.16942 0,-10.31926 -0.85994,-16.33882 -1.2899,-27.08806 14.18898,-20.20854 -0.85994,-15.90886 6.01957,-11.60917 -0.42997,-11.1792 0,-3.86972 17.19876,-8.59938 25.79815,1.28991 8.59938,8.59938 6.01957,0.85994 10.74923,11.60916 4.29969,14.61895 12.89907,6.87951 14.18898,0.42997 0.85994,161.66839 z").attr(attr),
	    	name: "Livland"};
	svgmap.estland = {path: R.path("m 693.54019,250.46977 -4.72966,3.00979 1.28991,5.5896 -10.74923,-5.15963 -6.87951,-1.28991 -2.57981,-10.31926 -12.03914,-6.44954 -8.16941,3.00979 5.15963,3.86972 0,5.5896 -7.30947,0.42997 -3.00979,5.15963 -8.59938,3.86972 c 0,0 -0.85994,8.59938 -0.85994,6.44953 0,-2.14984 -14.18898,2.57982 -14.18898,2.57982 l -6.01957,6.87951 -0.85994,14.18898 -7.73944,1.2899 -0.85994,-10.31926 4.72966,-6.8795 -11.1792,-8.16941 1.71988,-9.88929 -2.14984,-11.60917 2.57981,-0.42997 3.00978,6.87951 8.16942,-2.14985 -0.42997,-7.30947 6.8795,-3.43976 10.31926,0 0,-7.73944 -8.16941,4.72966 -3.86972,-2.14985 -0.85994,-10.74922 -6.87951,-4.2997 -7.30947,-1.2899 -0.85994,-4.72966 12.03914,-0.42997 7.73944,-10.74923 16.33883,5.5896 1.71987,6.8795 5.5896,3.00979 10.31926,-8.16942 4.72966,3.43976 2.14985,-5.15963 -7.30948,-6.44954 5.15963,-6.01957 -4.29969,-6.8795 21.06849,-4.72966 -0.42997,-5.5896 -4.72966,-0.42997 0,-4.72966 11.17919,-2.14985 4.2997,-0.85993 3.00978,-3.00979 3.43975,0 z").attr(attr),
	    	name: "Estland"};
	svgmap.nyland = {path: R.path("m 692.25028,108.14999 -5.58959,-0.42997 3.00978,5.15963 -2.14985,7.73945 -9.02935,-3.86972 -6.8795,3.86972 -7.73945,-0.42997 -6.8795,7.30947 -10.74923,0 -16.33883,18.48868 -5.5896,-2.14985 0.42997,-9.88929 9.45932,-6.8795 -0.85994,-7.73945 -8.59938,-4.29969 -5.5896,13.75901 -6.01956,0 -4.2997,-7.30947 -0.42996,-7.73945 -15.90886,-1.71987 -24.93821,22.35839 -47.2966,12.4691 -16.7688,3.00979 -11.1792,-6.44954 -11.17919,-31.81771 10.31925,-12.899077 51.16633,-7.739444 19.34861,0 2.57982,-3.009784 -3.43976,-3.869722 2.14985,-12.039135 -5.15963,-6.879506 2.14984,-8.169413 -0.42997,-7.309475 7.73945,-2.149846 -5.15963,-8.169413 131.14058,-0.429969 z").attr(attr),
	name: "Nyland"};
	svgmap.norge = {path: R.path("m 55.036047,267.66854 8.599383,-9.88929 6.019568,0 0,-6.44954 7.739444,-1.71988 0,-10.31925 7.739444,-0.42997 2.149845,-8.16942 9.459321,0 -0.859938,-6.01956 7.739446,-2.14985 3.43975,-12.89907 3.43975,4.72966 15.04892,0.42997 8.59939,0 -0.42997,-13.32905 6.44953,-6.8795 0.85994,-11.1792 -7.73944,-5.15963 0.85994,-9.02935 14.61895,3.00978 0,-4.72966 -2.57982,-3.43975 9.02935,-0.85994 -5.15963,9.45932 -1.71987,9.88929 4.72966,19.77858 12.4691,4.29969 1.28991,3.43976 1.28991,50.30638 7.30947,8.16942 2.14985,10.74922 -3.43976,3.86973 11.1792,12.03913 11.60917,-0.85994 1.71987,-10.31926 3.00979,-0.85993 1.71987,-13.32905 -9.88929,-4.29969 0,-15.47889 -2.14984,-6.44953 0,-13.32905 -4.29969,-6.44953 -1.71988,-15.47889 5.15963,-3.86972 0,-4.72966 8.59938,-0.85994 1.71988,-3.43975 -3.00978,-27.08806 0.42996,-5.5896 7.73945,0 10.74923,-15.90886 6.01956,-1.71987 -3.00978,-27.94799 1.71988,-12.03914 -6.44954,-15.478888 -1.28991,-9.459321 -6.01956,-15.908857 6.8795,-6.019568 9.45932,3.009784 1.28991,-3.009784 3.86972,-3.439753 -4.29969,-15.478888 -169.407833,-0.429969 z").attr(attr),
	name: "Norge"};
	
	for (var state in svgmap) {
		$.ajax({
			  url: "areadata",
			  dataType: 'json',
			  async: false,
			  data: {area: svgmap[state].name},
			  success: 	function(data) {
	        		if (data) {
        			    svgmap[state].data = data;
        		}
			  }
			});
		(function (st, state) {
            st[0].onmouseover = function () {
                st.animate({"fill-opacity" : 0.5},0);
                $("#info_over").html(state.data[0]);
                st.toFront();
                R.safari();
            };
            st[0].onmouseout = function () {
            	st.animate({"fill-opacity" : 0},0);
            	$("#info_over").html("Inget");
                st.toFront();
                R.safari();
            };
            st[0].onclick = function () {
            	var output = "Namn: " + state.data[0] + "<br>";
            	output += "Befolkning: " + state.data[1] + "<br>";
            	output += "Skog: " + state.data[2] + "<br>";
            	output += "Åkermark: " + state.data[3] + "<br>";
            	output += "Betesmark: " + state.data[4] + "<br>";
            	output += "Järn: " + state.data[5] + "<br>";
            	output += "Silver: " + state.data[6] + "<br>";
            	output += "Fisk: " + state.data[7];
                $("#info_main").html(output);
            };
		})(svgmap[state].path, svgmap[state]);
	}
    $("#info_over").html("Färdigladdat!");
}

function drawCrosses() {
	var canvas = document.getElementById("cross2");  
    var ctx = canvas.getContext("2d");
    ctx.strokeStyle = "#ffffff";
    ctx.lineWidth = 3;
    ctx.beginPath();  
    ctx.moveTo(0,12);  
    ctx.lineTo(40,12);  
    ctx.stroke(); 
    ctx.beginPath();  
    ctx.moveTo(20,0);  
    ctx.lineTo(20,25);  
    ctx.stroke(); 

	var canvas = document.getElementById("cross3");  
    var ctx = canvas.getContext("2d");
    ctx.strokeStyle = "#ffffff";
    ctx.lineWidth = 3;
    ctx.beginPath();  
    ctx.moveTo(0,0);  
    ctx.lineTo(40,25);  
    ctx.stroke(); 
    ctx.beginPath();  
    ctx.moveTo(40,0);  
    ctx.lineTo(0,25);  
    ctx.stroke(); 
}


function drawFlag(id)  {
    var canvas = document.getElementById(id);  
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = playerData.flag_color;  
    ctx.fillRect (0, 0, 160, 100);  
    ctx.strokeStyle = "#ffffff";
    ctx.lineWidth = 11;
    if (playerData.flag_cross === "Normal") {
	    ctx.beginPath();  
	    ctx.moveTo(0,50);  
	    ctx.lineTo(160,50);  
	    ctx.stroke(); 
	    ctx.beginPath();  
	    ctx.moveTo(80,0);  
	    ctx.lineTo(80,100);  
	    ctx.stroke(); 
    }
    if (playerData.flag_cross === "Corner") {
	    ctx.beginPath();  
	    ctx.moveTo(0,0);  
	    ctx.lineTo(160,100);  
	    ctx.stroke(); 
	    ctx.beginPath();  
	    ctx.moveTo(160,0);  
	    ctx.lineTo(0,100);  
	    ctx.stroke(); 
    }
}